//
//  XGL_LiveRoomUserItemView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/29.
//

import UIKit

class XGL_LiveRoomUserItemView: UIView {
    
    var userHeadView: UserHeadView!
    var userNameText: UILabel!
    var followItem: UIButton!
    var userId: String = ""
    var isSelf: Bool = false{
        didSet{
            if isSelf{
                self.createSelfUI()
            }else{
                self.createOtherUI()
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.24)
        self.layerCornerRadius = 22.0
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension XGL_LiveRoomUserItemView{
    @objc private func followItemAction(sender: UIButton){
        sender.isSelected = !sender.isSelected
        self.followRequest(id: self.userId, asFollow: sender.isSelected)
    }
}

extension XGL_LiveRoomUserItemView{
    private func followRequest(id: String,asFollow: Bool){
        PanRequestManager<XGL_RequestBaseResponse<Response>,XGL_CustomRequestError>().request(XGLPersonalRequestAPI(requestAPI: .Follow(userId: id, isFollow: asFollow))) { result in
            switch result{
            case .success:
                break
            default:
                break
            }
        }
    }
}

extension XGL_LiveRoomUserItemView{
    private func createSelfUI(){
        
        self.userHeadView = UserHeadView().then({ make in
            make.layerCornerRadius = 15.0
            make.clipsToBounds = true
        })
        self.userNameText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
        })
        self.addSubviews([userHeadView,userNameText])
        userHeadView.snp.makeConstraints { make in
            make.left.equalTo(5.0)
            make.width.height.equalTo(30.0)
            make.centerY.equalToSuperview()
        }
        userNameText.snp.makeConstraints { make in
            make.left.equalTo(userHeadView.snp.right).offset(5.0)
            make.centerY.equalToSuperview()
        }
        self.snp.makeConstraints { make in
            make.right.equalTo(userNameText.snp.right).offset(15.0)
            make.height.equalTo(36.0)
        }
    }
    
    private func createOtherUI(){
        
        self.userHeadView = UserHeadView().then({ make in
            make.layerCornerRadius = 15.0
            make.clipsToBounds = true
        })
        self.userNameText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
        })
        self.followItem = UIButton().then({ make in
            make.setTitle("Follow", for: .normal)
            make.setTitle("UnFollow", for: .selected)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.layerCornerRadius = 12.5
            make.addTarget(self, action: #selector(self.followItemAction(sender:)), for: .touchUpInside)
        })
        self.addSubviews([userHeadView,userNameText,self.followItem])
        userHeadView.snp.makeConstraints { make in
            make.left.equalTo(5.0)
            make.width.height.equalTo(30.0)
            make.centerY.equalToSuperview()
        }
        userNameText.snp.makeConstraints { make in
            make.left.equalTo(userHeadView.snp.right).offset(5.0)
            make.centerY.equalToSuperview()
        }
        self.followItem.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.height.equalTo(25.0)
            make.width.equalTo(conversionWithScrennWidth(value: 63.0))
            make.left.equalTo(userNameText.snp.right).offset(5.0)
        }
        self.snp.makeConstraints { make in
            make.right.equalTo(followItem.snp.right).offset(6.0)
            make.height.equalTo(36.0)
        }
    }
}
